package reader.contorller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import reader.entity.Book;
import reader.entity.Category;
import reader.service.BookService;
import reader.utils.ResponseUtils;

import java.util.List;

@RestController
@RequestMapping("/api/book")
public class BookController {
    @Autowired
    private BookService bookService;

    @GetMapping("/list")
    public ResponseUtils list(@RequestParam("categoryId") Long categoryId, @RequestParam("order") String order,@RequestParam("page") Integer page) {
        System.out.println("in?");
        ResponseUtils response = null;
        try {
            IPage<Book> bookIPage = bookService.selectPage(categoryId, order, page, 10);
            response = new ResponseUtils().put("data", bookIPage);
        }catch (Exception e){
            e.printStackTrace();
            response = new ResponseUtils().put("error",e.getMessage());
        }
        return response;
    }

    @GetMapping("/id/{id}")
    public ResponseUtils findById(@PathVariable("id") Long id){
        ResponseUtils response = null;
        try {
            Book book = bookService.selectBookById(id);
            response = new ResponseUtils().put("data",book);
        }catch (Exception e){
            e.printStackTrace();
            response = new ResponseUtils().put("error",e.getMessage());
        }
        return response;
    }
}
